package com.dangdang.util;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

/**
 * Hibernate 工具类
 * 
 * @author Wangpeihu
 * @team [Never give up]
 * @since JDK1.6(建议)
 * @date 2010-1-19
 */
public class HbnUtils {
	private static SessionFactory sf = null;
	static {
		// Hibernate配置文件可以随便放,但是要声明路径,如果放在src目录下就不用些路径,系统默认是该路径
		// 如果一个项目中使用两个DB,写两个hibernate工具类
		String path = "/com/dangdang/util/hibernate.cfg.xml";
		try {
			sf = new Configuration().configure(path).buildSessionFactory();
		} catch (HibernateException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 获取session
	 * 
	 * @return Session
	 */
	public static Session getSession() {
		Session s = null;
		try {
			s = sf.openSession();
		} catch (HibernateException e) {
			e.printStackTrace();
		}
		return s;
	}
}